#include "EnemyFactory.h"
#include <time.h>

CEnemyFactory* CEnemyFactory::m_EnemyFactory = NULL;

CEnemyFactory::CEnemyFactory()
{
	memset(m_wBirthCount,0,sizeof(m_wBirthCount));
}

CEnemyFactory::~CEnemyFactory(void)
{
	if(!m_EnemyTank.empty())
		m_EnemyTank.clear();
}

CEnemyFactory* CEnemyFactory::GetFactory()
{
	if(m_EnemyFactory == NULL)
		m_EnemyFactory = new CEnemyFactory();
	return m_EnemyFactory;
}

CTank* CEnemyFactory::CreateTank()
{
	srand(GetTickCount());
	int birthRand = rand()%3;
	int dir = DIRECTION_DOWN;
	for(int i=  0; i < BIRTH_COUNT; i++)
	{
		if(m_wBirthCount[i] == 0)
		{
			birthRand = i;
			break;
		}
	}
	switch(birthRand)
	{
	case BIRTH_LEFTTOP:
		dir = rand()%2 +1;
		break;
	case BIRTH_MIDTOP:
		dir = rand()%3+1;
		break;
	case BIRTH_RIGHTTOP:
		dir = rand()%2 +2;
		break;
	default:
		break;
	}

	CResManager *res = CResManager::GetInstance();
	CTank* enemy = new CTank(70.0,BIRTHPOINT[birthRand][0],BIRTHPOINT[birthRand][1], dir, 
		res->m_spriteEnemy[0],res->m_spriteEnemy[1]);
	enemy->InitRole(2.0, ENEMYMAXLV1,GROUP_ENEMY, 0,2,2,2);
	m_EnemyTank.push_back(enemy);

	m_wBirthCount[birthRand]++;

	return enemy;
}

size_t CEnemyFactory::GetEnemyCount()
{
	return m_EnemyTank.size();
}

void CEnemyFactory::CloseFactory()
{
	if(m_EnemyFactory)
		delete m_EnemyFactory;
	m_EnemyFactory = NULL;
}

std::list<CTank* >* CEnemyFactory::GetAllEnemy()
{
	return &(GetFactory()->m_EnemyTank);
}